define(["jquery", "easy-admin"], function ($, ea) {

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'crm.customer/index',
        add_url: 'crm.customer/add',
        edit_url: 'crm.customer/edit',
        atedit_url: 'crm.customer/atedit',
        seasadd_url: 'crm.customer/seasadd',
        delete_url: 'crm.customer/delete',
        import_url: 'crm.customer/import',
        export_url: 'crm.customer/export',
        modify_url: 'crm.customer/modify',
    };

    cols_fields=[];
    var Controller = {
        index: function () {
            if(CONFIG.cols_fields){
                //{search:'<input type="hidden" name="scope" value="1">',hide:true},
                cols_fields=[].concat({'type': 'checkbox'},CONFIG.cols_fields,{'width': 200, 'title': '操作','fixed':'right', 'templet': ea.table.tool,operat: [
                        [
                            {
                                class: 'layui-btn layui-btn-xs layui-btn-primary',
                                method: 'open',
                                text: '写跟进',
                                auth: 'dialogue',
                                url: 'crm.record/dialogue',
                                extend: 'data-full="true"', icon: 'fa fa-plus ',
                            },
                        ],'edit', 'delete',
                    /*
                        [

                            {
                                class: 'layui-btn layui-btn-xs layui-btn-primary',
                                method: 'open',
                                text: '上传合同',
                                auth: 'dialogue',
                                url: 'crm.contract/dialogue',
                                extend: 'data-full="true"', icon: 'fa fa-plus ',
                            }
                        ],
                        */
                        ]})
            }
            ea.table.render({
                toolbar: ['refresh','add','delete','export',[{
                    text: '导入',
                    url: init.import_url,
                    method: 'open',
                    auth: 'import',
                    class: 'layui-btn layui-btn-success layui-btn-sm',
                    icon: 'fa fa-upload ',
                    extend: '',
                }],[{
                    text: '移入公海',url:'client/to_move_gh',
                    method: 'url',
                    auth: 'to_move_gh',
                    class: 'layui-btn layui-btn-danger layui-btn-sm',title:'确定要将勾选的客户移入公海吗？',
                    icon: 'fa fa-industry',extend:'data-checkbox="true"'
                }],[{
                    text: '转移客户',url:'crm.customer/alter_pr_user',
                    method: 'open',
                    auth: 'alter_pr_user',
                    class: 'layui-btn layui-btn-primary layui-btn-sm',title:'确定要将勾选的客户转移吗？',
                    icon: 'fa fa-clock-o',extend:'data-checkbox="true"'
                }],[{
                    text: '自定义设置',
                    url: 'system.fields/index?table=crm_customer',
                    method: 'open',
                    auth: 'fields',
                    class: 'layui-btn layui-btn-warm layui-btn-sm',
                    icon: 'fa fa-cogs ',
                    extend: 'data-full="true"',
                }]],
                init: init,limit:CONFIG.ADMINPAGESIZE,
                cols:[ cols_fields],
                done: function(res, curr, count){
                    if(count===undefined && res.msg && res.url){
                        ea.msg.tips(res.msg,1,function (){
                            window.top.location.href=res.url;
                        })
                    }
                },where: {scope: 1}
            });
            ea.listen();

        },
        allindex: function () {
            if(CONFIG.cols_fields){
                cols_fields=[].concat({'type': 'checkbox'},CONFIG.cols_fields)
            }
            ea.table.render({
                url:'crm.customer/allindex',
                toolbar: ['refresh','export'],
                init: init,limit:CONFIG.ADMINPAGESIZE,
                cols:[ cols_fields],
                done: function(res, curr, count){
                    if(count===undefined && res.msg && res.url){
                        ea.msg.tips(res.msg,1,function (){
                            window.top.location.href=res.url;
                        })
                    }
                },where: {scope: 1}
            });
            ea.listen();

        },
        seas: function () {
            if(CONFIG.cols_fields){
                cols_fields=[].concat({'type': 'checkbox'},CONFIG.cols_fields,{'width': 100, 'title': '操作',toolbar: '#action',fixed: 'right'})
            }
            ea.table.render({url:'crm.customer/seas',
                toolbar: ['refresh',[{
                    text: '添加客户',url:'crm.customer/seasadd',
                    method: 'open',
                    auth: 'seasadd',
                    class: 'layui-btn layui-btn-normal layui-btn-sm',
                    icon: 'fa fa-plus'
                }],'delete',[{
                    text: '转移客户',url:'crm.customer/seas_alter_pr_user',
                    method: 'open',
                    auth: 'seas_alter_pr_user',
                    class: 'layui-btn layui-btn-primary layui-btn-sm',title:'确定要将勾选的客户转移吗？',
                    icon: 'fa fa-clock-o',extend:'data-checkbox="true"'
                }]],
                init: init,limit:CONFIG.ADMINPAGESIZE,
                cols:[ cols_fields],
                done: function(res, curr, count){
                    if(count===undefined && res.msg && res.url){
                        ea.msg.tips(res.msg,1,function (){
                            window.top.location.href=res.url;
                        })
                    }
                }
            });
            ea.listen();

        },
        //流转客户
        tmp_user: function () {
            if(CONFIG.cols_fields){
                cols_fields=[].concat({'type': 'checkbox'},CONFIG.cols_fields,{'width': 182, 'title': '操作','fixed':'right', 'templet': function (row) {
                        var operat = [];

                        operat.push([
                            {
                                class: 'layui-btn layui-btn-primary layui-btn-xs',
                                method: 'open',
                                text: '转移',
                                auth: 'atedit',
                                url: 'crm.customer/seas_alter_pr_user',
                                extend: 'data-full="true"',
                            }
                        ])


                        if (operat.length > 0) {
                            return ea.table.tool(row, {
                                init, // 初始化的数据变量
                                operat: operat
                            })
                        }
                    }})
            }
            ea.table.render({url:'crm.customer/tmp_user',
                toolbar: ['refresh','add','delete','export',[{
                    text: '导入',
                    url: init.import_url,
                    method: 'open',
                    auth: 'import',
                    class: 'layui-btn layui-btn-success layui-btn-sm',
                    icon: 'fa fa-upload ',
                    extend: '',
                }],[{
                    text: '移入公海',url:'client/to_move_gh',
                    method: 'url',
                    auth: 'to_move_gh',
                    class: 'layui-btn layui-btn-danger layui-btn-sm',title:'确定要将勾选的客户移入公海吗？',
                    icon: 'fa fa-industry',extend:'data-checkbox="true"'
                }],[{
                    text: '转移客户',url:'crm.customer/alter_pr_user',
                    method: 'open',
                    auth: 'alter_pr_user',
                    class: 'layui-btn layui-btn-primary layui-btn-sm',title:'确定要将勾选的客户转移吗？',
                    icon: 'fa fa-clock-o',extend:'data-checkbox="true"'
                }],[{
                    text: '自定义设置',
                    url: 'system.fields/index?table=crm_customer',
                    method: 'open',
                    auth: 'fields',
                    class: 'layui-btn layui-btn-warm layui-btn-sm',
                    icon: 'fa fa-cogs ',
                    extend: 'data-full="true"',
                }]],
                init: init,limit:CONFIG.ADMINPAGESIZE,
                cols:[ cols_fields],
                done: function(res, curr, count){
                    if(count===undefined && res.msg && res.url){
                        ea.msg.tips(res.msg,1,function (){
                            window.top.location.href=res.url;
                        })
                    }
                },where: {scope: 1}
            });
            ea.listen();

        },
        at_user: function () {
            if(CONFIG.cols_fields){
                cols_fields=[].concat({'type': 'checkbox'},CONFIG.cols_fields,{'width': 120, 'title': '操作','fixed':'right', 'templet': function (row) {
                        var operat = [];

                        operat.push([
                            {
                                class: 'layui-btn layui-btn-success layui-btn-xs',
                                method: 'open',
                                text: '查看',
                                auth: 'atedit',
                                url: 'crm.record/view',
                                extend: 'data-full="true"',
                            }
                        ])

                        if (row.pr_user == row.at_user || !row.pr_user) {
                            operat.push([
                                {
                                    class: 'layui-btn layui-btn-success layui-btn-xs',
                                    method: 'open',
                                    text: '编辑',
                                    auth: 'atedit',
                                    url: 'crm.customer/atedit',
                                    extend: 'data-full="true"',
                                }
                            ])
                        }

                        if ((row.pr_user != '' || row.pr_user_bef != '') && parseInt(row.yx_status) === 0) {
                            operat.push([
                                {
                                    class: 'layui-btn layui-btn-danger layui-btn-xs',
                                    method: 'open',
                                    text: '申诉',
                                    auth: 'atedit',
                                    url: 'crm.customer/appeal',
                                    extend: 'data-full="true"',
                                }
                            ])
                        }


                        if (operat.length > 0) {
                            return ea.table.tool(row, {
                                init, // 初始化的数据变量
                                operat: operat
                            })
                        }
                    }})
            }
            ea.table.render({url:'crm.customer/at_user',
                toolbar: ['refresh','add'],
                init: init,limit:CONFIG.ADMINPAGESIZE,
                cols:[ cols_fields],
                done: function(res, curr, count){
                    if(count===undefined && res.msg && res.url){
                        ea.msg.tips(res.msg,1,function (){
                            window.top.location.href=res.url;
                        })
                    }
                }
            });
            ea.listen();

        },
        issuccess: function () {
            if(CONFIG.cols_fields){
                cols_fields=[].concat(CONFIG.cols_fields)
            }
            ea.table.render({url:'crm.customer/issuccess',
                toolbar: ['refresh'],
                init: init,limit:CONFIG.ADMINPAGESIZE,
                cols:[ cols_fields],
                done: function(res, curr, count){
                    if(count===undefined && res.msg && res.url){
                        ea.msg.tips(res.msg,1,function (){
                            window.top.location.href=res.url;
                        })
                    }
                }
            });
            ea.listen();

        },
        add: function () {
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
        import: function () {
            ea.listen();
        },
        alter_pr_user: function () {
            ea.listen();
        },
        seas_alter_pr_user: function () {
            ea.listen();
        },
        seasadd: function () {
            ea.listen();
        },
        atedit: function () {
            ea.listen();
        },
        appeal:function (){
            ea.listen();
        }
    };
    return Controller;
});
